package chances.webui.taglibs;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.lang.StringUtils;

import chances.webui.model.Validator;

@SuppressWarnings("serial")
public class ValidatorTag extends BodyTagSupport {
	Validator vaildator;

	@Override
	public void setPageContext(PageContext pageContext) {
		this.vaildator = new Validator();
		super.setPageContext(pageContext);
	}

	@Override
	public int doEndTag() throws JspException {
		InputTag tag = (InputTag) findAncestorWithClass(this, InputTag.class);
		FormTag formTag = (FormTag) findAncestorWithClass(tag, FormTag.class);
		
		if(formTag !=null && tag!=null){
			tag.setRequired(this.vaildator.isRequired() || this.vaildator.getSelected());
			String field = tag.getName();
			if(StringUtils.isNotEmpty(field)){
				formTag.addValidator(field,this.vaildator);
			}
			if(tag instanceof DatepickerTag){
				if(StringUtils.isNotEmpty(((DatepickerTag)tag).getBeginName())){
					formTag.addValidator(((DatepickerTag)tag).getBeginName(),this.vaildator);
				}
				if(StringUtils.isNotEmpty(((DatepickerTag)tag).getEndName())){
					formTag.addValidator(((DatepickerTag)tag).getEndName(),this.vaildator);
				}
			}
		}
		
		ColumnTag columnTag = (ColumnTag) findAncestorWithClass(this, ColumnTag.class);
		TableTag tableTag = (TableTag) findAncestorWithClass(columnTag, TableTag.class);
		if(columnTag !=null && tableTag!=null){
			columnTag.setRequired(this.vaildator.isRequired());
			String field = columnTag.getField();
			if(StringUtils.isNotEmpty(field)){
				tableTag.addValidator(field,this.vaildator);
			}
		}
		
		if(this.bodyContent!=null){
			if(this.vaildator.getMessages()==null){
				this.setMessages(this.bodyContent.getString());
			}
		}
		return SKIP_BODY;
	}

	public void setRequired(boolean required) {
		vaildator.setRequired(required);
	}

	public void setIdentity(String identity) {
		vaildator.setIdentity(identity);
	}

	public void setEmail(boolean email) {
		vaildator.setEmail(email);
	}

	public void setNumber(boolean number) {
		vaildator.setNumber(number);
	}

	public void setMobile(boolean mobile) {
		vaildator.setMobile(mobile);
	}

	public void setDigits(boolean digits) {
		vaildator.setDigits(digits);
	}

	public void setEqualTo(String equalTo) {
		vaildator.setEqualTo(equalTo);
	}

	public void setAccept(String accept) {
		vaildator.setAccept(accept);
	}

	public void setMaxlength(String maxlength) {
		vaildator.setMaxlength(maxlength);
	}

	public void setMinlength(String minlength) {
		vaildator.setMinlength(minlength);
	}

	public void setRangelength(String rangelength) {
		vaildator.setRangelength(rangelength);
	}

	public void setRange(String range) {
		vaildator.setRange(range);
	}

	public void setMax(String max) {
		vaildator.setMax(max);
	}

	public void setMin(String min) {
		vaildator.setMin(min);
	}

	public void setRegexper(String regexper) {
		vaildator.setRegexper(regexper);
	}

	public void setMessages(String message) {
		vaildator.setMessages(message);
	}

	public void setRemote(String remote) {
		vaildator.setRemote(remote);
	}

	public void setSelected(boolean selected) {
		vaildator.setSelected(selected);
	}

}
